package testJdk.thread.pool.scheduledExecutorServiceTest;


import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

/**
 * @author Created by cyd on 2021-03-29 17:54:42
 * @Description : 测试线程池的定时任务
 */
public class ScheduledExecutorServiceTest01 {
    public static void main(String[] args) {
        // 创建一个延迟执行的线程池
        ScheduledExecutorService ses = Executors.newScheduledThreadPool(2);
        for (int i = 0; i < 10; i++) {
            ScheduledFuture<?> schedule = ses.schedule(new MyRunnable(i), 2, TimeUnit.SECONDS);
        }
        System.out.println("over");
    }
}

class MyRunnable implements Runnable {
    private int id;

    public MyRunnable(int id) {
        this.id = id;
    }

    @Override
    public void run() {
        String name = Thread.currentThread().getName();
        System.out.println(name + "-执行了任务:" + id);
    }
}
